<?php

class GuestbookController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $guestbook = new Application_Model_GuestbookMapper();
        $this->view->entries = $guestbook->fetchAll();
    }

    public function signAction()
    {
        $request = $this->getRequest();
        $form    = new Application_Form_Guestbook();

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($request->getPost())) {
                $formValues = $form->getValues();
                $comment = new Application_Model_Guestbook($formValues);
                $mapper  = new Application_Model_GuestbookMapper();
                $mapper->save($comment);
                if ($formValues) {
                    return $this->_forward('sendmail', 'guestbook', $form->getValues());
                }
                return $this->_helper->redirector('guestbook');
            }
        }

        $this->view->form = $form;
    }
    
    public function sendmailAction()
    {
        $bootstrap = $this->getInvokeArg('bootstrap'); 
        $options = $bootstrap->getOptions();
        
        $addressTo = $options["mail"]["address"];
        $paramEmail = $this->_getParam('email');
        $paramComment = $this->_getParam('comment');
        $message = "E-Mail: " . $paramEmail . PHP_EOL . "Comment: " . $paramComment;
        
        $tr = new Zend_Mail_Transport_Smtp('localhost');
        
        $this->view->comment = $this->_getParam('comment');
        $this->view->email = $this->_getParam('email');
        $mail = new Zend_Mail('UTF-8');
        $mail->setBodyText($this->view->render('guestbook/mail.phtml'))
            ->setFrom($addressTo, 'Ein Versender')
            ->addTo($addressTo, 'Ein Empfänger')
            ->setSubject('New comment in your guestbook!')
            ->send($tr);
        echo '$bla';
        
        return $this->_helper->redirector('index');
    }


}